/*
 * Copyright (c) 2001, Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Author: Adam Dunkels <adam@sics.se>
 *
 * $Id: tapdev.c,v 1.8 2006/06/07 08:39:58 adam Exp $
 */

#define UIP_DRIPADDR0   192
#define UIP_DRIPADDR1   168
#define UIP_DRIPADDR2   0
#define UIP_DRIPADDR3   1
#include <config.h>
#include <compiler.h>
#include <common.h>
#include <net.h>
#include <asm/io.h>
#include <asm/arch/platform.h>

#include "uip.h"
#include "uip_arp.h"

DECLARE_GLOBAL_DATA_PTR;
static int drop = 0;
static int fd;

#define led1_iocfg_reg     iocfg_reg51
#define led1_reg_base      REG_BASE_GPIO0

#define iocfg_reg51 0x120c0020

/*---------------------------------------------------------------------------*/
void
tapdev_init(void)
{
	int ret, i;
	struct uip_eth_addr net_ethaddr;
	//u8 net_ethaddr[6];

	if (!eth_current) {
		puts("No ethernet found.\n");
		return -ENODEV;
	}//在hieth_register_dev(UP_PORT)时注册了一个eth
/*	if (eth_get_dev())
	{
	    memcpy(&net_ethaddr, eth_get_ethaddr(), 6);
	    printf("mac is \n");
	    for(i=0; i<6; i++)
	    {
	        printf("%02x ", net_ethaddr.addr[i]);
	    }
	    printf("\n ");
	}
	else
	    printf("mac not fiound");
	uip_setethaddr(net_ethaddr);
*/	
	i = eth_current->init(eth_current, gd->bd);
	printf("eth init code is %d\n", i);

	printf("Trying %s\n", eth_current->name);
//	httpd_setstatu(1);
	
	while(1)
	{
	    i = eth_rx();
	    printf("eth rx code is %d\n", i);
/*	    if(i == 0)
	    {
	        httpd_getpacket(uip_buf, &ret);
	        
	        printf("DST MAC is\n");
	        for(i=0; i<6; i++)
	        {
	            printf("%02x ", *(uip_buf+i));
	        }	
	        printf("\n");
	        
	        printf("SUR MAC is\n");
	        for(; i<12; i++)
	        {
	            printf("%02x ", *(uip_buf+i));
	        } 
	        printf("\n");
	        
	        printf("PRO is\n");  
	        for(; i<14; i++)
	        {
	            printf("%02x ", *(uip_buf+i));
	        }
	        printf("\n");
	        
	        printf("data is\n");
	        for(; i<ret; i++)
	        { 
	            printf("%u ", *(uip_buf+i));
	            if(((i-14)%32) == 31) printf("\n");
	        }   
	        printf("\n");    
	    }*/
	    udelay(2000000);
	}

/*
	if (eth_current->init(eth_current, gd->bd) >= 0) {
		eth_current->state = ETH_STATE_ACTIVE;
		httpd_setstatu(1);
		printf("tapdev init success!\n");
	}
	else printf("Tapdev init fail!\n");
*/
}
/*---------------------------------------------------------------------------*/
unsigned int
tapdev_read(void)
{
  int ret, i;
  i = eth_rx();
  if( i== 0)
  {
      httpd_getpacket(uip_buf, &ret);
      
      printf("read data\n");
      printf("DST MAC is\n");
	  for(i=0; i<6; i++) printf("%02x ", *(uip_buf+i));
	  printf("\n");
	        
	  printf("SUR MAC is\n");
	  for(; i<12; i++) printf("%02x ", *(uip_buf+i));
	  printf("\n");
	  
	  printf("PRO is\n");  
	  for(; i<14; i++) printf("%02x ", *(uip_buf+i));
	  printf("\n");
	  
	  return ret;
  }
  else //timeout
  {
      //printf("Time out\n");
      return 0;
  }
  //printf("tapdev_read\n");
}
/*---------------------------------------------------------------------------*/
void
tapdev_send(void)
{
    int ret, i ;
    
    printf("send data\n");
    printf("DST MAC is\n");
	for(i=0; i<6; i++) printf("%02x ", *(uip_buf+i));
	printf("\n");
	        
	printf("SUR MAC is\n");
	for(; i<12; i++) printf("%02x ", *(uip_buf+i));
	printf("\n");
	  
	printf("PRO is\n");  
	for(; i<14; i++) printf("%02x ", *(uip_buf+i));
	printf("\n");
	  
    ret = eth_send(uip_buf, uip_len);
    if (ret == 0) printf("tapdev_send success\n");
    else printf("tapdev_send fail\n");
    
}
/*---------------------------------------------------------------------------*/
